package com.zhixing.service.processor;

import com.zhixing.model.SwaggerDefinitionModelAttr;
import com.zhixing.model.SwaggerRequest;
import lombok.Getter;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * swagger 入参 json 解析处理器
 * @author zhixing
 */
public class SwaggerRequestProcessor implements SwaggerProcessor{
    /** json 解析后的 入参对象 **/
    @Getter
    private List<SwaggerRequest>    resultList;

    @Getter
    private List<LinkedHashMap>     parameters;

    @Getter
    private Map<String, SwaggerDefinitionModelAttr> definitinMap;

    private SwaggerRequestProcessor(){
        this.resultList = new ArrayList<>();
    }

    public static Builder createBuild(){
        return  new Builder();
    }

    public static class Builder{
        private SwaggerRequestProcessor processor;
        public Builder(){
            this.processor = new SwaggerRequestProcessor();
        }

        public Builder parameters(List<LinkedHashMap> parameters){
            this.processor.parameters = parameters;
            return this;
        }

        public Builder definitinMap(Map<String, SwaggerDefinitionModelAttr> definitinMap){
            this.processor.definitinMap = definitinMap;
            return this;
        }

        public SwaggerRequestProcessor build(){
            /*if(null == processor.definitinMap || CollectionUtils.isEmpty(processor.parameters)){
                throw new SwaggerProcessorRunTimeException("SwaggerReuestProcessor build error");
            }*/
            return this.processor;
        }

    }

    @Override
    public SwaggerRequestProcessor process() throws IOException {
        doProcess();
        return this;
    }

    /**
     * 处理请求参数列表
     *
     * @return
     */
    private void doProcess() {
        if (!CollectionUtils.isEmpty(parameters)) {
            for (Map<String, Object> param : parameters) {
                Object in = param.get("in");
                SwaggerRequest request = new SwaggerRequest();
                request.setName(String.valueOf(param.get("name")));
                request.setType(param.get("type") == null ? "object" : param.get("type").toString());
                /*if (param.get("format") != null) {
                    request.setType(request.getType() + "(" + param.get("format") + ")");
                }*/
                request.setParamType(String.valueOf(in));
                // 考虑对象参数类型
                if (in != null && "body".equals(in)) {
                    request.setType(String.valueOf(in));
                    Map<String, Object> schema = (Map) param.get("schema");
                    Object ref = schema.get("$ref");
                    // 数组情况另外处理
                    if (schema.get("type") != null && "array".equals(schema.get("type"))) {
                        ref = ((Map) schema.get("items")).get("$ref");
                        request.setType("array");
                    }
                    if (ref != null) {
                        request.setType(request.getType() + ":" + ref.toString().replaceAll("#/definitions/", ""));
                        request.setModelAttr(definitinMap.get(ref));
                    }
                }
                // 是否必填
                request.setRequire(false);
                if (param.get("required") != null) {
                    request.setRequire((Boolean) param.get("required"));
                }
                // 参数说明
                request.setRemark(String.valueOf(param.get("description")));
                resultList.add(request);
            }
        }
    }


}
